package com.siqi.hr.system.custom.dao.mysql;

import com.siqi.hr.system.common.authority.Sysdata;
import com.siqi.hr.system.common.sql.STable;
import com.siqi.hr.system.custom.dao.intf.EntityRecordDao;
import com.siqi.hr.system.custom.entity.EntityRecord;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 实体注册信息维护dao实现类
 * @author    xiaogq
 * Created by xiaogoingqin on 2016/7/24.
 */
@Component("entityRecordDao")
public class EntityRecordDaoImpl implements EntityRecordDao {
    private static final Logger logger = Logger.getLogger(EntityRecordDaoImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询所有的系统对象或者自定义对象(xiaogq)
     * @param systemEntity      查询系统对象还是自定义对象标识(true:系统对象，false：自定义对象)
     * @return
     */
    public List<Map<String, Object>> getEntityRecords(boolean systemEntity) throws Exception{
        StringBuffer sql = new StringBuffer("SELECT sc.table_code as 'id',sc.`name`,sc.inchinese,sc.belong_mudule,sc.permission,sc.define_set,sc.system_entity,");
        sql.append("sc.define_field,sc.define_approval,sc.use_recyclebin,sc.define_stage_manager,sc.define_type,sc.define_logic,sc.define_message,sc.position ");
        sql.append(" from sys_custom_entity as sc where sc.system_entity = ? and sc.nodisplay=0 and sc.set_element <> 1 order by ISNULL(position), position asc");

        List<Map<String,Object>> mapList = jdbcTemplate.queryForList(sql.toString(),new Object[] { systemEntity});

        return mapList;
    }

    @Override
    public EntityRecord queryEntityRecordById(int entityId) throws Exception{
        StringBuffer sql = new StringBuffer("select * from sys_custom_entity where table_code = "+entityId);
        EntityRecord entityRecord = jdbcTemplate.queryForObject(sql.toString(), new EntityRecord.FullEntityRecord());
        return entityRecord;
    }

    @Override
    public boolean tableExistedInDatabase(Integer entityId) {
        String sql = "select count(*) from information_schema.`TABLES` where TABLE_SCHEMA = '" + Sysdata.getInstance().getDatabase() + "' and TABLE_NAME = '" + STable.TABLE_HEAD + entityId + "'";
        int b = 0;
        try {
            b = this.jdbcTemplate.queryForObject(sql,Integer.class);
        } catch (DataAccessException e) {
        }
        return b > 0;
    }

    @Override
    public int updateEntityRecord(EntityRecord entity) {
        String sql = "update sys_custom_entity set name = ?, inchinese = ? where table_code = ? ";
        int counts = jdbcTemplate.update(sql,new Object[] { entity.getName(),entity.getInchinese(),entity.getTableCode()});
        return counts;
    }

    @Override
    public void deleteEntityRecord(Integer entityId) {
        String sql = "delete from sys_custom_entity where table_code = ? ";
        jdbcTemplate.update(sql,new Object[] { entityId });
    }

    @Override
    public int getEntityTableRecodeCount(Integer entityId) {
        String sql = "select count(id) from " +STable.TABLE_HEAD + entityId;
        int count = jdbcTemplate.queryForObject(sql,Integer.class);
        return count;
    }

    @Override
    public List<EntityRecord> getCanQuoteEntity(boolean bool) {
        String sql = "select table_code,name,inchinese from sys_custom_entity where can_quote = "+bool;
        return jdbcTemplate.query(sql,new EntityRecord.mainEntityRecord());
    }

    @Override
    public List<EntityRecord> queryPmsEntity(boolean bool) {
        String sql = " select table_code,name,inchinese from sys_custom_entity where permission = "+bool;
        return this.jdbcTemplate.query(sql,new EntityRecord.mainEntityRecord());
    }

}
